<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(-1)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column label="岗位名称">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="onUser(scope.row)">{{ scope.row.name }}</el-button>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
                <template #default="scope">
                    <el-button link type="primary" @click="onEdit(scope.$index)" size="small">编辑</el-button>
                    <el-button link type="warning" @click="onStatus(scope.row.id)" size="small">禁用</el-button>
                    <el-popconfirm title="确定要删除么?" @confirm="onDelete(scope.row.id)">
                        <template #reference>
                            <el-button link type="danger" size="small">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

    <el-dialog v-model="isAuthForm" title="权限管理" width="60%">
        <component :is="authForm" :data = editId :type="1" @onSave="onSave" />
    </el-dialog>

    <el-dialog v-model="isUser" title="人员管理" width="60%">
        <component :is="user" :data = editId @onSaveUser="onSaveUser" />
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import { CirclePlus, RefreshRight, Search } from '@element-plus/icons-vue'
import authForm from './components/authForm.vue';
import user from './components/user.vue';
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
const indexMethod = (index: number) => {
    return index+= 1
}
const form = ref<any>({search:'',type:1,page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
const isAuthForm = ref<boolean>(false)
const isUser = ref<boolean>(false)
interface Role {
    id: number
    name: string
    auth: string
    users: number
    created_at: string
    status: string
    type:number
}
const editId = ref<number>(0)
const tableData = ref<Role[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/role',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    onGetData()
}


const onUser = (data: Role) => {
    editId.value = data.id
    isUser.value = true
}

const onEdit = (data: number) => {
    editId.value = 0
    if(data >= 0){
        editId.value = tableData.value[data].id
    }
    isAuthForm.value = true
}

const onSave = () => {
    isAuthForm.value = false
    onGetData()
}

const onSaveUser = () => {
    isUser.value = false
    onGetData()
}

const onStatus = (id: number) => {
    http.patch('/role/status',{id:id}).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}

const onDelete = (id: number) => {
    http.delete('/role/'+id).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
}
onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>